package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddCate;
import com.itheima.ydd.mapper.YddCateMapper;
import com.itheima.ydd.service.IYddCateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;


@Service
public class YddCateServiceImpl extends ServiceImpl<YddCateMapper, YddCate> implements IYddCateService {
    /**
     * @description: 分页查询及条件查询
     **/
    @Override
    public ResultData getList(String catename, String page, String limit) {

        //构造查询条件
        LambdaQueryWrapper<YddCate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(catename), YddCate::getCatename,catename);

        Page<YddCate> pageInfo = page(new Page<YddCate>(Integer.parseInt(page), Integer.parseInt(limit)), queryWrapper);

        HashMap<String,Object> map = new HashMap<>();
        map.put("totalPages",limit);
        map.put("pageSize",pageInfo.getSize());
        map.put("currentPage",pageInfo.getCurrent());
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getRecords());

        return ResultData.ok("查询失败",map);
    }

    /**
     * @description: 获取分类详细信息
     **/
    @Override
    public ResultData getDetail(Integer id) {
        YddCate ydd = getById(id);
        return ResultData.ok("获取失败",ydd) ;
    }

    /**
     * @description: 修改分类信息
     **/
    @Override
    public ResultData updateYddCate(YddCate yddCate) {
        updateById(yddCate);
        return ResultData.ok("修改成功！",null);
    }

    /**
     * @description: 删除分类
     **/
    @Override
    public ResultData delete(Integer id) {
        removeById(id);
        return ResultData.ok("删除成功！",null);
    }

    /**
     * @description: 添加新分类
     **/
    @Override
    public ResultData add(YddCate yddCate) {
        save(yddCate);
        return ResultData.ok("添加成功！",null);
    }

    /**
     * @description: 分类信息-全部
     **/
    @Override
    public ResultData getAll() {
        List<YddCate> list = list();
        return ResultData.ok("未获得数据",list);
    }

}
